package com.ocom.srvpaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.access.EntityMjDeviceInfoExtend;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.CommonUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.srvpaccess.dao.IDaoMjDeviceInfoExtend;
import com.ocom.srvpaccess.service.MjDeviceInfoExtendService;
import org.springframework.stereotype.Service;


@Service
public class MjDeviceInfoExtendServiceImpl extends ServiceImpl<IDaoMjDeviceInfoExtend, EntityMjDeviceInfoExtend> implements MjDeviceInfoExtendService {


    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public EntityMjDeviceInfoExtend getExtendInfo(Long companyId, String  deviceNo) {

        QueryWrapper<EntityMjDeviceInfoExtend> query = Wrappers.query();
        query.eq("device_no",deviceNo);
        query.eq("com_id",companyId);
        EntityMjDeviceInfoExtend entityMjDeviceInfoExtend = this.getOne(query);

        if(entityMjDeviceInfoExtend==null){
            entityMjDeviceInfoExtend=new EntityMjDeviceInfoExtend();
            entityMjDeviceInfoExtend.setComId(companyId);
            entityMjDeviceInfoExtend.setDeviceNo(deviceNo);
            entityMjDeviceInfoExtend.setUpdateWhiteInfo(0);
            this.save(entityMjDeviceInfoExtend);
        }
        return entityMjDeviceInfoExtend;
    }





    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean saveExtendInfo(Long companyId, EntityMjDeviceInfoExtend entityMjDeviceInfoExtend) {
        UpdateWrapper<EntityMjDeviceInfoExtend> update = Wrappers.update();
        update.eq("device_no",entityMjDeviceInfoExtend.getDeviceNo());
        update.eq("com_id",entityMjDeviceInfoExtend.getComId());
        if(!CommonUtil.isNull(entityMjDeviceInfoExtend.getWhiteUrl()))update.set("white_url",entityMjDeviceInfoExtend.getWhiteUrl());
        if(null!=entityMjDeviceInfoExtend.getWhiteUser())update.set("white_user",entityMjDeviceInfoExtend.getWhiteUser());
        if(null!=entityMjDeviceInfoExtend.getUpdateWhiteInfo())update.set("update_white_info",entityMjDeviceInfoExtend.getUpdateWhiteInfo());
        if(null!=entityMjDeviceInfoExtend.getWhiteFace())update.set("white_face",entityMjDeviceInfoExtend.getWhiteFace());
        if(null!=entityMjDeviceInfoExtend.getWhiteErrFace())update.set("white_err_face",entityMjDeviceInfoExtend.getWhiteErrFace());
        return this.saveOrUpdate(entityMjDeviceInfoExtend,update);
    }
}
